-- RestRoomM
-- Created by chengb May/28/2015
-- 休息室

module("RestRoomM", package.seeall);

-- 休息室配置
local restRoomTable = {};
local restRoomList  = {};

-- 内部函数声明
local whenLeaveDungeon;
local whenPassInstance;

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 转换休息室配置表
    local rest_room_CSV = get_rest_room_CSV();
    for i = 1, #rest_room_CSV do
        local id = rest_room_CSV[i].id;
        restRoomTable[id] = rest_room_CSV[i];
    end
    rest_room_CSV = nil;

    -- 根据id升序
    restRoomList = table.values(restRoomTable);
    table.sort(restRoomList, function(a, b) return a.id < b.id; end);
end

-- 初始化
function init()
    loadCsv();

    -- 关注玩家离开地牢的事件
    EventMgr.removeAll("RestRoomM");
    EventMgr.register("RestRoomM", event.LEAVE_DUNGEON, function()
        whenLeaveDungeon();
    end);

    -- 注册玩家通关副本的回调
    EventMgr.register("RestRoomM", event.INSTANCE_DISAPPER, function(args)
        whenPassInstance(args);
    end);
end

-- 检索配置
function query(id, path)
    if not restRoomTable[id] then
        return nil;
    end

    if path == "" or not path then
        return restRoomTable[id];
    end

    return restRoomTable[id][path];
end

-- 根据副本id查找休息室id
function findRoomByInstanceId(instanceId)
    for _, room in pairs(restRoomList) do
        if room["instance_id"] == instanceId then
            return room["id"];
        end
    end
    return nil;
end

-- 是否通关了所有的休息室
function isPassAllRestRoom()
    local passRestRoom = ME.user.dbase:query("pass_rest_room") or {};
    for _, room in pairs(restRoomList) do
        if table.indexOf(passRestRoom, room["id"]) == -1 then
            return false;
        end
    end
    return true;
end

-- 离开地牢的回调
function whenLeaveDungeon()
    -- 清除休息室统计
    if ME.user.dbase:query("rest_room_stat") ~= nil then
        ME.user.dbase:delete("rest_room_stat");
    end
end

-- 是否在休息室中
function isInRestRoom()
    local instanceInfo = DungeonInstanceM.getCurInstanceInfo();
    if not instanceInfo then
        return false;
    end

    return instanceInfo.type == INSTANCE_TYPE_ROOM;
end

-- 获取当前所在的休息室id
function getCurRestRoomId()
    if not isInRestRoom() then
        -- 不在休息室中
        return nil;
    end

    local instanceInfo = DungeonInstanceM.getCurInstanceInfo();
    local instanceId = instanceInfo.id;

    -- 根据副本id找到休息室id
    for _, room in pairs(restRoomList) do
        if room["instance_id"] == instanceId then
            return room["id"];
        end
    end

    return nil;
end

-- 抽取休息室副本
function fetchRestRoom()
    local selectList = {};
    local restRoomStat = ME.user.dbase:query("rest_room_stat", {});
    local seed = DungeonM.getRandSeed("fetch_rest_room");

    for _, mks in pairs(restRoomList) do
        -- 当前已出现次数
        local curTimes = restRoomStat[mks.id] or 0;

        local info = table.deepcopy(mks);

        -- 如果该休息室不可重复出现，并且已经出现过，则将权重设为0
        if info.unrepeat == 1 and curTimes > 0 then
            info.ratio = 0;
        end

        -- 迷宫小关不出现，则将权重设为0
        local dungeonId = DungeonM.getDungeonId();
        if type(dungeonId) == "number" and dungeonId > 0 and
           DungeonAreaM.query(dungeonId, "type") == "child" and
           table.indexOf({ 1, 2, }, mks.id) ~= -1 then
            info.ratio = 0;
         end

        -- 加入候选列表
        table.insert(selectList, info);
    end

    -- 根据权重抽取
    local ret = fetchElemBySeed(selectList, seed);
    local id = ret.id;

    -- 更新抽取记录
    restRoomStat[id] = restRoomStat[id] or 0;
    restRoomStat[id] = restRoomStat[id] + 1;
    ME.user.dbase:set("rest_room_stat", restRoomStat);

    local instanceId = query(id, "instance_id");

    return instanceId;
end

-- 更新休息室通关记录
function updatePassRecord(roomId)
    if nil == query(roomId) then
        -- 休息室不存在
        return;
    end

    local passRestRoom = ME.user.dbase:query("pass_rest_room") or {};
    if table.indexOf(passRestRoom, roomId) ~= -1 then
        -- 已经记录过了
        return;
    end

    table.insert(passRestRoom, roomId);
    ME.user.dbase:set("pass_rest_room", passRestRoom);

    printTable(passRestRoom, "休息通关记录已刷新");
end

-- 玩家通关副本的回调
function whenPassInstance(args)
    local instanceId   = args.id;

    -- 根据副本id找到休息室id
    local roomId = findRoomByInstanceId(instanceId);
    if not roomId then
        -- 不是休息室副本
        return;
    end

    -- 记录通关的休息室
    updatePassRecord(roomId);

    -- 抛出休息室通关事件
    EventMgr.fire(event.REST_ROOM_PASS, { ["roomId"] = roomId, });
end



